﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

namespace ZXthex.Html
{
    public class UGUI_HtmlNodeOutline
    {
        static readonly string[] outline_order = { "up", "right", "down", "left" };


        Image[] outlineImgs;

        public UGUI_HtmlNodeOutline(Transform front)
        {
            outlineImgs = new Image[outline_order.Length];
            for (int i = 0; i < outlineImgs.Length; i++)
            {
                outlineImgs[i] = new GameObject("outline " + outline_order[i], typeof(RectTransform)).AddComponent<Image>();
                outlineImgs[i].rectTransform.SetParent(front);
                UGUI_Utility.SetFullSize(outlineImgs[i].rectTransform);
                switch (outline_order[i])
                {
                    case "up":
                        outlineImgs[i].rectTransform.anchorMin = new Vector2(0, 1);
                        outlineImgs[i].rectTransform.anchorMax = new Vector2(1, 1);
                        outlineImgs[i].rectTransform.sizeDelta = new Vector2(0, -0);
                        break;
                    case "right":
                        outlineImgs[i].rectTransform.anchorMin = new Vector2(1, 0);
                        outlineImgs[i].rectTransform.anchorMax = new Vector2(1, 1);
                        outlineImgs[i].rectTransform.sizeDelta = new Vector2(-0, 0);
                        break;
                    case "down":
                        outlineImgs[i].rectTransform.anchorMin = new Vector2(0, 0);
                        outlineImgs[i].rectTransform.anchorMax = new Vector2(1, 0);
                        outlineImgs[i].rectTransform.sizeDelta = new Vector2(0, 0);
                        break;
                    case "left":
                        outlineImgs[i].rectTransform.anchorMin = new Vector2(0, 0);
                        outlineImgs[i].rectTransform.anchorMax = new Vector2(0, 1);
                        outlineImgs[i].rectTransform.sizeDelta = new Vector2(0, 0);
                        break;
                }

                outlineImgs[i].color = Color.gray;
            }
        }

        public void SetColor(Color c)
        {
            for (int x = 0; x < outlineImgs.Length; x++)
            {
                outlineImgs[x].color = c;
            }
        }

        public void SetOutlineWidth(int width_px)
        {
            for (int i = 0; i < 4; i++)
            {
                switch (outline_order[i])
                {
                    case "up":
                        outlineImgs[i].rectTransform.sizeDelta = new Vector2(0, width_px);
                        break;
                    case "right":
                        outlineImgs[i].rectTransform.anchoredPosition = new Vector2(-width_px, 0);
                        outlineImgs[i].rectTransform.sizeDelta = new Vector2(width_px, 0);
                        break;
                    case "down":
                        outlineImgs[i].rectTransform.anchoredPosition = new Vector2(0, width_px);
                        outlineImgs[i].rectTransform.sizeDelta = new Vector2(0, width_px);
                        break;
                    case "left":
                        outlineImgs[i].rectTransform.sizeDelta = new Vector2(width_px, 0);
                        break;
                }

            }
        }
    }
}